package com.example.demo.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.demo.R;
import com.example.demo.myrouteplan.ChooseActivity;

import net.sf.json.JSONObject;

import cn.leancloud.AVLogger;
import cn.leancloud.AVOSCloud;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private Button register,login;
    private EditText username;
    private EditText password;


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 主要是添加下面这句代码
        MultiDex.install(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);



        register = findViewById(R.id.register);
        login=findViewById(R.id.login);
        username = findViewById(R.id.accountEdit);
        password = findViewById(R.id.passwordEdit);
//        AVOSCloud.setLogLevel(AVLogger.Level.DEBUG);
//        AVOSCloud.initialize(
//                this,
//                "gYKKKJUqrlSsLCUDRD1aNRHj-gzGzoHsz",
//                "viezs5eeEG4dLaPay20EaEXd",
//                "dwxr3lun.api.lncld.net"
//        );
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
//                if(username.getText().toString().isEmpty()||password.getText().toString().isEmpty() ){
//                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
//                }else{
//                    new Thread(new Runnable() {
//                        @Override
//                        public void run() {
//                            try{
//                                String json="{\n" +
//                                        "    \"username\":\""+
//                                        username.getText().toString() +
//                                        "\",\n" +
//                                        "    \"password\":\""+
//                                        password.getText().toString() +
//                                        "\"\n" +
//                                        "}";
//                                OkHttpClient client =new OkHttpClient();//创建http客户端
//                                Request request= new Request.Builder()
//                                        .url("http://101.201.124.181:8989/login/insert")
//                                        .post(RequestBody.create(MediaType.parse("application/json"),json))
//                                        .build();//创造http请求
//                                Response response = client.newCall(request).execute();//执行发送
//                                String stringResponse=response.body().string();
//
//                                try{
//                                    JSONObject jsonObject=JSONObject.fromObject(stringResponse);
//                                    String getBody=jsonObject.get("body").toString();
//                                    System.out.println(getBody+"----------------------");//
//                                    runOnUiThread(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//                                        }
//                                    });
//                                }catch(Exception e){
//                                    runOnUiThread(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
//                                        }
//                                    });
//                                }
//
//                            }catch(Exception e){
//                                e.printStackTrace();
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(LoginActivity.this, "网络连接错误", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                            }
//                        }
//                    }).start();
//                }
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(username.getText().toString().isEmpty()||password.getText().toString().isEmpty() ){
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    System.out.println("hello");
                    System.out.println("test----|"+username.getText().toString()+"|");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                String json="{\n" +
                                        "    \"username\":\""+
                                        username.getText().toString() +
                                        "\",\n" +
                                        "    \"password\":\""+
                                        password.getText().toString() +
                                        "\"\n" +
                                        "}";
                                System.out.println(json);//
                                OkHttpClient client =new OkHttpClient();//创建http客户端
                                Request request= new Request.Builder()
                                        .url("http://101.201.124.181:8989/login/login2")
                                        .post(RequestBody.create(MediaType.parse("application/json"),json))
                                        .build();//创造http请求
                                Response response = client.newCall(request).execute();//执行发送
                                String stringResponse=response.body().string();

                                try{
                                    JSONObject jsonObject=JSONObject.fromObject(stringResponse);
                                    String gettedId=jsonObject.get("id").toString();
                                    System.out.println(gettedId+"----------------------");//
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                            Intent intent=new Intent(LoginActivity.this, ChooseActivity.class);
                                            startActivity(intent);
                                        }
                                    });
                                }catch(Exception e){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }



                            }catch(Exception e){
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginActivity.this, "网络连接错误", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }).start();
                }
            }
        });
    }
}